package com.interview.salesorder.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.interview.core.page.Page;
import com.interview.orderdetail.entity.OrderDetail;
import com.interview.orderdetail.entity.OrderDetailListForm;
import com.interview.orderdetail.query.OrderlistConditionQuery;
import com.interview.salesorder.entity.SalesOrder;
import com.interview.salesorder.query.SalesOrderConditionQuery;
import com.interview.salesorder.service.SalesorderService;

@Controller
@RequestMapping(value = "salesorder")
public class SalesorderController {
	@Resource
	SalesorderService salesorderService;

	@RequestMapping(value = "page", method = RequestMethod.GET)
	public String page(ModelMap map, SalesOrderConditionQuery conditionQuery) {
		Page page = salesorderService
				.findSalesOrderBySalesOrderConditionQuery(conditionQuery);

		Integer pageSize = page.getPageSize();
		Integer pageIndex = page.getPageIndex();

		Integer total = page.getTotal();

		Integer totalPage = page.getTotalPage();

		if (pageIndex >= totalPage & totalPage != 0) {
			pageIndex = totalPage - 1;
		}

		List<SalesOrder> salesOrders = page.getList();

		map.put("pageIndex", pageIndex);
		map.put("pageSize", pageSize);
		map.put("totalPage", totalPage);
		map.put("list", salesOrders);
		map.put("conditionQuery", conditionQuery);

		return "salesorder/page";
	}

	@RequestMapping(value = "{id}/view", method = RequestMethod.GET)
	public String veiw(ModelMap map, @PathVariable String id) {

		SalesOrder salesOrder = salesorderService.findOneSalesOrder(id);
		List<OrderDetail> orderLists = salesOrder.getOrderList();

		map.put("salesOrder", salesOrder);
		map.put("list", orderLists);

		return "salesorder/view";
	}

	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String toAdd(ModelMap map) {

		List<OrderDetail> orderLists = new ArrayList<OrderDetail>();

		orderLists.add(null);

		map.put("list", orderLists);
		return "salesorder/add";
	}

	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String add(ModelMap map, SalesOrder salesOrder,
			OrderDetailListForm orderDetails) {

		salesorderService.saveBySalesOrderAndOrderListForm(salesOrder,
				orderDetails);

		return "redirect:/salesorder/page";
	}

	@RequestMapping(value = "{id}/edit", method = RequestMethod.GET)
	public String toEidt(ModelMap map, @PathVariable String id,
			OrderlistConditionQuery conditionQuery) {

		SalesOrder salesOrder = salesorderService.findOneSalesOrder(id);
		List<OrderDetail> orderLists = null;

		if (conditionQuery.getSalesOrderId() != null) {
			orderLists = salesorderService
					.findOrderListByOrderlistConditionQuery(conditionQuery);
		} else {
			orderLists = salesOrder.getOrderList();
		}

		map.put("list", orderLists);
		map.put("salesOrder", salesOrder);

		return "salesorder/edit";
	}

	@RequestMapping(value = "edit", method = RequestMethod.PUT)
	public String edit(ModelMap map, OrderDetailListForm orderDetails,
			SalesOrder salesOrder) {
		salesorderService.updateBySalesOrderAndOrderListForm(salesOrder,
				orderDetails);
		return "redirect:/salesorder/page";
	}

	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
	public String delete(ModelMap map, @PathVariable String id) {
		salesorderService.deleteSalesOrder(id);
		return "redirect:/salesorder/page";
	}

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));// true:允许输入空值，false:不能为空值
	}

	// -------------------------------------------------------------------------------
	
	@RequestMapping(value="cyclemap" , method=RequestMethod.GET)

	public String cycleMap(ModelMap map) {
	
	
	List reList = new ArrayList<HashMap>();
	
	Map map2 = new HashMap();
	map2.put("name", "jack");
	map2.put("sum_pcost", 25.2);
	reList.add(map2);
	int i = 5;
	List<Integer>kk = new ArrayList<Integer>();
	kk.add(0);
	kk.add(1);
	kk.add(2);
	kk.add(3);
	kk.add(4);
	map.put("list2", reList);
	map.put("long", map2);
	map.put("list3", kk);
	map.put("index", 2);
	return "salesorder/cyclemap";
	}

}
